//DTO 对象 实现api的接口，从DTO转为DO
package service

import (
	"context"
	"store-assistant/api/pbs"
	"store-assistant/internal/biz"
	"strconv"

	"github.com/google/wire"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
)

type Warehouse interface {
	// 读取订单详情
	GetItemInfoWithId(context.Context, *pbs.GetItemInfoWithIdRequest) (*pbs.GetItemInfoWithIdResponse, error)

	OnSaleNewItem(context.Context, *pbs.OnSaleNewItemRequest) (*pbs.OnSaleNewItemResponse, error)
	// 读取某一分类的所有货物详情
	GetItemsWithCategory(context.Context, *pbs.GetItemsWithCategoryRequest) (*pbs.GetItemsWithCategoryResponse, error)
	// 商家货物
	OnSaleItem(context.Context, *pbs.OnSaleItemRequest) (*emptypb.Empty, error)
	// 下架货物
	HaltSaleItem(context.Context, *pbs.HaltSaleItemRequest) (*emptypb.Empty, error)
	// 增加库存
	IncrItemsInventory(context.Context, *pbs.IncrItemsInventoryRequest) (*emptypb.Empty, error)
	// 减少库存
	DecrItemsInventory(context.Context, *pbs.DecrItemsInventoryRequest) (*emptypb.Empty, error)
}

var Provider = wire.NewSet(NewWarehouseService, wire.Bind(new(pbs.WarehouseServiceServer), new(*WarehouseService)))

type WarehouseService struct {
	dao biz.WarehouseRepo
}

func NewWarehouseService(repo biz.WarehouseRepo) (service *WarehouseService, cf func(), err error) {
	service = &WarehouseService{
		dao: repo,
	}
	cf = func() {}
	return
}

func (w *WarehouseService) _convertItemInfoToBizItem(info *pbs.ItemInfo) *biz.Item {
	i := &biz.Item{}
	i.ItemId = info.GetId().GetId()
	i.Name = info.GetName()
	i.Desc = info.GetDesc()
	i.Inventory = info.GetInventory()
	i.Category = info.GetCategory()
	i.Status, _ = strconv.Atoi(info.GetItemStat())
	return i
}

func (w *WarehouseService) _convertBizItemToItemInfo(item *biz.Item) *pbs.ItemInfo {
	// ...... convert
	return &pbs.ItemInfo{}
}

// 读取订单详情
func (w *WarehouseService) GetItemInfoWithId(ctx context.Context, req *pbs.GetItemInfoWithIdRequest) (*pbs.GetItemInfoWithIdResponse, error) {
	//问题： 虽然这层是做DTO到DO的转换的，但是这个里面的转换细节一般要怎么做呢？

	do := biz.NewWarehouseDomain(req.GetId().GetShopId(), w.dao)
	item, err := do.GetItemWithId(req.GetId().GetId())
	if err != nil {
		return &pbs.GetItemInfoWithIdResponse{}, err
	}

	return &pbs.GetItemInfoWithIdResponse{
		Info: w._convertBizItemToItemInfo(item),
	}, nil
}

// 读取某一分类的所有货物详情
func (w *WarehouseService) GetItemsWithCategory(ctx context.Context, req *pbs.GetItemsWithCategoryRequest) (*pbs.GetItemsWithCategoryResponse, error) {
	return nil, status.Error(codes.Unimplemented, codes.Unimplemented.String())
}

func (w *WarehouseService) OnSaleNewItem(ctx context.Context, req *pbs.OnSaleNewItemRequest) (*pbs.OnSaleNewItemResponse, error) {
	do := biz.NewWarehouseDomain(req.GetId().GetShopId(), w.dao)
	id, err := do.OnSaleNewItem(w._convertItemInfoToBizItem(req.GetInfo()))
	if err != nil {
		return &pbs.OnSaleNewItemResponse{}, err
	}

	return &pbs.OnSaleNewItemResponse{
		Id: &pbs.ItemId{
			Id:     id,
			ShopId: do.ShopId,
		},
	}, err
}

// 上架货物
func (w *WarehouseService) OnSaleItem(ctx context.Context, req *pbs.OnSaleItemRequest) (*emptypb.Empty, error) {
	return nil, status.Error(codes.Unimplemented, codes.Unimplemented.String())
}

// 下架货物
func (w *WarehouseService) HaltSaleItem(ctx context.Context, req *pbs.HaltSaleItemRequest) (*emptypb.Empty, error) {
	return nil, status.Error(codes.Unimplemented, codes.Unimplemented.String())
}

// 增加库存
func (w *WarehouseService) IncrItemsInventory(ctx context.Context, req *pbs.IncrItemsInventoryRequest) (*emptypb.Empty, error) {
	return nil, status.Error(codes.Unimplemented, codes.Unimplemented.String())
}

// 减少库存
func (w *WarehouseService) DecrItemsInventory(ctx context.Context, req *pbs.DecrItemsInventoryRequest) (*emptypb.Empty, error) {
	return nil, status.Error(codes.Unimplemented, codes.Unimplemented.String())
}
